package org.Panel;

import com.google.gson.Gson;
import org.JsonMsg.PersonalMsg;
import org.Tools.OnLineOP;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

public class PersonalPanel extends FaPanel{

    //用户名
    JLabel usernameLabel = new JLabel("用户名：");
    String username = "";
    //我的帖子/获取历史记录
    JLabel MyPost = new JLabel("我的历史记录");
//    String myPost = "";
    //我的回帖
    JLabel MyReply = new JLabel("我的回帖");
//    String myReply = "";
    //发帖按钮
    JLabel publishPost = new JLabel("发布帖子");
    //发帖数
    JLabel MyPostSum  = new JLabel("发帖数：");
    String myPostSum = "10";
    //回帖数/改为活跃度
    JLabel MyReplySum = new JLabel("活跃度：");
    public String myReplySum = "20";
    //头像
    JButton HeadPortrait = new JButton("头像");
    //权限
    JLabel Power = new JLabel("权限：");
    String power = "普通成员";
    //返回主页按钮
    JLabel mainPostPanel = new JLabel("返回主页");
    //母控件大小接口
    PanelSize panelSize;
    //母控件跳转接口
    PanelSkip panelSkip;
    //json
    Gson gson = new Gson();


    public PersonalPanel(PanelSkip panelSkip, PanelSize panelSize) {

        //获取接口
        this.panelSkip = panelSkip;
        this.panelSize = panelSize;
        //界面索引
        PanelIndex = 3;

        //属性设置
        setBounds(0,0,panelSize.getWidth(),panelSize.getHeight());
        setLayout(null);

        //头像
        HeadPortrait.setLocation(10,10);
        HeadPortrait.setSize(50,50);

        //用户名
        usernameLabel.setLocation(320,10);
        usernameLabel.setSize(300,50);

        //我的发帖/获取历史记录
        MyPost.setBounds(10,80,100,50);
        MyPost.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                String msg = "getHistory-a-"+MainPanel.userName;
                System.out.println(msg);
                OnLineOP onLineOP = new OnLineOP();
                String jsonmsg;
                try {
                    jsonmsg = onLineOP.sendone(msg);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                //服务器端获取的数据
                MainPanel.AllPost = jsonmsg;
                panelSkip.skip(4);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //鼠标移动至文本中,改变鼠标指针样式
                MyPost.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                //将文字颜色改为蓝色
                MyPost.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                //当鼠标移出文本时,回复文字颜色黑色
                MyPost.setForeground(Color.BLACK);
            }
        });

        //我的回帖
        MyReply.setBounds(320,80,100,50);
        MyReply.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                MyReply.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                MyReply.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                MyReply.setForeground(Color.BLACK);
            }
        });

        //发布帖子
        publishPost.setBounds(450,300,100,50);
        publishPost.addMouseListener(new MouseAdapter() {
            int f = 1;
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    MainPanel.post=null;
                    panelSkip.skip(6);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
                publishPost.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                publishPost.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
                publishPost.setForeground(Color.BLACK);
            }
        });

        //回帖数
        MyPostSum.setBounds(10,150,300,50);
        //发帖数
        MyReplySum.setBounds(320,150,300,50);
        //权限
        Power.setBounds(10,220,300,50);
        //返回主页按钮
        mainPostPanel.setBounds(320,220,300,50);
        mainPostPanel.addMouseListener(new MouseAdapter() {
            int f =1;
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    String msg;
                    String jsonmsg;
                    OnLineOP onLineOP = new OnLineOP();
                    //发送给服务器的数据
                    msg = "getAllPost-a-"+MainPanel.userName;
                    //服务器端获取的数据
                    try {
                        jsonmsg = onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    try {
                        jsonmsg = onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    MainPanel.AllPost = jsonmsg;
                    panelSkip.skip(4);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
                mainPostPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                mainPostPanel.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
                mainPostPanel.setForeground(Color.BLACK);
            }
        });

        add(HeadPortrait);
        add(usernameLabel);
        add(MyPost);
//        add(MyReply);
        add(publishPost);
//        add(MyPostSum);
        add(MyReplySum);
        add(Power);
        add(mainPostPanel);

        //头像
        HeadPortrait.setSize(50,50);
//        repaint();

    }

    @Override
    public void Run() {

        //更新各个信息
        usernameLabel.setText("用户名："+username);
        MyReplySum.setText("活跃度："+myReplySum);
//        MyPostSum.setText("发帖数："+myPostSum);
        Power.setText("权限："+power);

        //输出信息
//        System.out.println("personal panel run");
//        System.out.println(getWidth());
//        System.out.println(getHeight());

        //重绘界面
        repaint();

    }

    public void updatePanel() {

        String msg;
        String jsonmsg;
        OnLineOP onLineOP = new OnLineOP();

        //个人信息数据
        msg = "getUserIdByUser-"+MainPanel.userId+"-svc";
        System.out.println(msg);
        try {
            jsonmsg = onLineOP.sendone(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PersonalMsg personalMsg = new PersonalMsg();//个人信息类
        personalMsg = gson.fromJson(jsonmsg, PersonalMsg.class);

        username = personalMsg.getUsername();
        myReplySum = String.valueOf(personalMsg.getInteractions());
        if(personalMsg.getRole().equals("USER")) {
            power = "普通成员";
        }else {
            power = "管理员";
        }

    }

}
